<?php
/**
 * 频道图片库 * 
 * 使用说明：需要传递自动参数  4个参数
 * limit 条数
 * topTagId 顶级标签
 * dateType(天数范围)默认1 * 天数范围规范：1 ：前7天
 * queryType(查询方式 预留参数)默认1
 * 
 * channelgallerycontroller.php
 * Create on 2015-04-15 13:12:23
 * Create by yuebin
 */
namespace haibao\cms\business\control\pc\channel;

use \haibao\cms\business\control\pc\entity\ImageCollectiveEntity as ImageCollectiveEntity;
use \haibao\cms\business\control\pc\action\ImageCollectiveAction;
use haibao\cms\web\view\app\article\Preview;
use \haibao\cms\business\control\pc\entity\ArticleEntity;
use \haibao\cms\business\control\pc\action\ArticleAction;

class ChannelGalleryController extends \haibao\cms\business\control\pc\channel\ChannelController{
	
	public function __construct($updateTime = null,$controlId,$isPublish){
	    $this->savaLog();
		parent::__construct($updateTime,$controlId,$isPublish);
		$this->initArticleAction();
	}
	
	/**
	 * 显示模版内容（模版动态数据的绑定和模版输出）
	 * @param : String $content
	 * @param : Array $controlData;
	 */
	public function displayTemplateHtml(){

		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		$channelName = array(
		    310=>'fashion',1383=>'stars',24=>'beauty',955=>'brands',6863=>'jewelrywatch',7455=>'accessory',520=>'life',211=>'wedding'
		);
		if($this->checkData($controlData)){
		    $this->assign('wordLinkList',$this->getWordLinkList($controlData['auto'],$controlData['manual']['topTagId']));
		    
			$this->parseTagId($controlData['manual']['topTagId']);
			$this->getFirstActualData($controlData['manual'],$channelName);
			$path = $this->generateHtmlPath(__CLASS__);
			$isNewHtml = $this->generateHtml($path,$content);
			$content = $this->getHtml($path);
			$content = $this->replaceCDNLink($content);
		}else{
			return $content = '<div></div>';
		}
		return $content;
	}
	
	public function getWordLinkList($autoData,$topId = ''){
	
	    $wordLinkList = array();
	    if($autoData){
	        foreach($autoData as $data){
	            array_push($wordLinkList,json_decode($data['ParameterValue'],true));
	        }
	    }else{
	        $cache = new \haibao\cms\cache\WordLink();
	        $wordTemp = (array)$cache->getWordLinkTag($topId);
	        if(!$wordTemp){
    	        $wordTemp = $this->getWordLinkArticle($topId);
	        }
	        $linkTemp = array();
            if($wordTemp){
                foreach($wordTemp as $key=>$val){
                    $linkTemp['wordUrl'] = $val;
                    $linkTemp['wordTitle'] = $key;
                    array_push($wordLinkList,$linkTemp);
                }
            }
	        
	    }
	    if($topId){
	        $temp = array('wordUrl'=>\haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_PIC).'/piclists/'.$topId.'/','wordTitle'=>'更多>>');
	        array_push($wordLinkList,$temp);
	    }
	    return $wordLinkList;
	}
	
	public function getWordLinkArticle($topId){
	    $articleEntity = new ArticleEntity();
	    $articleEntity->setLevel(3);
	    $articleEntity->setBigLevel(7);
	    $articleEntity->setLimit(5);
	    $articleEntity->setIsDate(true,1);
	    $articleEntity->setIsProImage(false);
	    $articleEntity->setIsLeftJoinTag(true);
	    $articleEntity->setIsPrimaryTag(true);
	    $articleEntity->setIsConditionTag(true);
	    $articleEntity->setIsParentTag(true);
	    $articleEntity->setIsReturnTag(true);
	    $articleEntity->setIsImageColl(true);
	    $articleEntity->setTagIds((array)$topId);
	    $articleEntity->setOrderByNew(false); 
	    $articleEntity->setOrderByCommentClick(true);
	    $articleEntity->setSource(__CLASS__);
	    $articleEntity->setIsDebug(false);
	    $articleEntity->setIsOnlyReturnPrimaryTag(false);
	    $this->articleAction->initArticleAction();
	    $this->articleAction->setArticleEnity($articleEntity);
	    list($articles,$imageColls) = $this->articleAction->getListForNew();
	    $articleTemp = $imageTemp = $autoWordLink = $arr2 = array();
	    if($articles['list']){
	        foreach($articles['list'] as $key=>$article){
	            if(isset($articles['tag'][$article->ArticleTag->TagId])){
	                $articleTemp[$key]['tagName'] = $articles['tag'][$article->ArticleTag->TagId]['TagName'];
	                $articleTemp[$key]['tagUrl'] = $articles['tag'][$article->ArticleTag->TagId]['Url']; //tagURl
	            }
	        }
	    }
	    if($imageColls['list']){
	        foreach($imageColls['list']  as $key=>$coll){
	            if(isset($imageColls['tag'][$coll->ImageCollectiveTag->TagId])){
	                $imageTemp[$key]['tagName'] = $imageColls['tag'][$coll->ImageCollectiveTag->TagId]['TagName'];
	                $imageTemp[$key]['tagUrl'] = $imageColls['tag'][$coll->ImageCollectiveTag->TagId]['Url'];
	            }
	        }
	    }
	    $autoWordLink = array_merge($articleTemp,$imageTemp);
	    if($autoWordLink){
	        foreach($autoWordLink as $k=>$v){
	            $arr1[$v['tagUrl']] = $v['tagName'];
	            $arr2 = array_flip($arr1);
	        }
	    }
	    $arr2 = array_slice($arr2,0,5,true);
	    $cache = new \haibao\cms\cache\WordLink();
	    $cache->setWordLinkTag($topId, $arr2);
        return $arr2;
	}
	
	
	/**
	 * 根据频道顶级标签获取图片库数据
	 */
	public function getFirstActualData($dataCondition,$channelName){
	    $articleEntity = new ArticleEntity();
        $articleEntity->setLevel(3);
        $articleEntity->setBigLevel(7);
        $articleEntity->setLimit(6);
        $articleEntity->setIsDate(true,3);
        $articleEntity->setIsProImage(true);
        $articleEntity->setIsLeftJoinTag(true);
        $articleEntity->setIsPrimaryTag(true);
        $articleEntity->setIsConditionTag(true);
        $articleEntity->setIsParentTag(true);
        $articleEntity->setIsReturnTag(true);
        $articleEntity->setIsImageColl(true);
        $articleEntity->setTagIds((array)$dataCondition['topTagId']);
        $articleEntity->setOrderByNew(true,'asc');
        $articleEntity->setSource(__CLASS__);
        $articleEntity->setIsDebug(false);
        $articleEntity->setIsOnlyReturnPrimaryTag(false);
        $articleEntity->setProImageLevel(\haibao\cms\model\data\ArticleProImage::Level_FOUR);
        $this->articleAction->initArticleAction();
        $this->articleAction->setArticleEnity($articleEntity);
        list($articles,$imageColls) = $this->articleAction->getListForNew();
        $mergeData = $this->mergeData($articles['list'],$imageColls['list'],$articleEntity->getLimit());
        $topTag = self::getTopTags();
        foreach($mergeData as $key=>$article){
            if($article->SiteType == \haibao\cms\business\control\pc\action\ArticleAction::INFO_TYPE_ARTICLE){//文章
                if(isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR])){
                    $article->AccessUrl = $articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR];
                }else {
                    $article->AccessUrl = $articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];  //图片地址
                }
                $article->ArticleUrl = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_PIC).'/'. $channelName[$article->ArticleTag->ParentId].'/article/'.$article->PublishId.'.htm';//$this->getArticleUrl($article->PublishId,$article->ArticleTag->ParentId); //文章地址

                if(isset($articles['tag'][$article->ArticleTag->TagId])){
                    $article->ArticleTag->TagName = $articles['tag'][$article->ArticleTag->TagId]['TagName'];
                    $article->ArticleTag->TagUrl = $articles['tag'][$article->ArticleTag->TagId]['Url']; //tagURl
                }
            }else{//图集
                if(isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\imagecollective\ImageCollectivePro::IMAGE_TYPE_FOUR])){
                    $article->AccessUrl = $imageColls['proImage'][$article->Id][\haibao\cms\model\data\imagecollective\ImageCollectivePro::IMAGE_TYPE_FOUR];
                }else {
                    $article->AccessUrl = $imageColls['proImage'][$article->Id][\haibao\cms\model\data\imagecollective\ImageCollectivePro::IMAGE_TYPE_ZERO]; //图片地址
                }
                $article->ImageCollectivePro = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_PIC).'/'.$topTag[$article->ImageCollectiveTag->ParentId]['channel'].'/pics/'.$article->ImageCollectiveTag->ImageCollectiveId.'.htm';
                if(isset($imageColls['tag'][$article->ImageCollectiveTag->TagId])){
                    $article->ImageCollectiveTag->TagName = $imageColls['tag'][$article->ImageCollectiveTag->TagId]['TagName'];
                    $article->ImageCollectiveTag->Url = $imageColls['tag'][$article->ImageCollectiveTag->TagId]['Url'];
                }
            }

        }
        $articleLists = $temp = array();
        if($mergeData){
            foreach($mergeData as $key=>$val){
                if($val->SiteType == \haibao\cms\business\control\pc\action\ArticleAction::INFO_TYPE_IMAGE){
                    $temp['Id'] = $val->Id;
                    $temp['ChannelUrl'] = $val->ImageCollectivePro;
                    $temp['TagName'] = $val->ImageCollectiveTag->TagName;
                    $temp['TagUrl'] =  $val->ImageCollectiveTag->Url;
                    $temp['ProImage'] = $val->AccessUrl;
                    $temp['Title'] = $val->Title;
                    $temp['SiteType'] = \haibao\cms\business\control\pc\action\ArticleAction::INFO_TYPE_IMAGE;
                }else{
                    $temp['Id'] = $val->Id;
                    $temp['ChannelUrl'] = $val->ArticleUrl;
                    $temp['TagName'] = $val->ArticleTag->TagName;
                    $temp['TagUrl'] =  $val->ArticleTag->TagUrl;
                    $temp['ProImage'] = $val->AccessUrl;
                    $temp['Title'] = $val->Title;
                    $temp['SiteType'] = \haibao\cms\business\control\pc\action\ArticleAction::INFO_TYPE_ARTICLE;
                }
                array_push($articleLists,$temp);
            }
        }
		$this->assign('imageColls', $articleLists);
		$this->assign('picHost', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_PIC));
	}
	

	private function checkData($controlData){
		if($controlData['manual']){
	
			if(isset($controlData['manual']['limit']) &&
			isset($controlData['manual']['topTagId']) &&
			isset($controlData['manual']['dateType']) 
	
			){
				return true;
	
			}else{
				return false;
			}
		}
	}
	
	private function initArticleAction(){
	    $this->articleAction = ArticleAction::initArticleAction();
	}
	
}